package com.carcontrol.rotate;


import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.widget.FrameLayout;
import android.widget.TextView;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import com.carcontrol.rotate.base.BaseActivity;
import com.carcontrol.rotate.util.Util_Rotate3DAnimation2;
import com.dalong.library.listener.OnItemClickListener;
import com.dalong.library.listener.OnItemSelectedListener;
import com.dalong.library.listener.OnLoopViewTouchListener;
import com.dalong.library.view.LoopRotarySwitchView;

public class Main2Activity extends BaseActivity {
    private FrameLayout framelayout;
    private FrameLayout framelayout2;
    LoopRotarySwitchView mLoopRotarySwitchView;


    private FragmentTransaction tration;
    Util_Rotate3DAnimation2 rotation;

    int width;
    int height;

    Fragment fromFragment;
    Fragment firstFragment;
    Fragment secondFragment;
    Fragment thirdFragment;

    float dx;
    float x;

    boolean first = true;
    boolean isEnd = false;

    private float degrees;
    private float degrees2;

    int pos;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

        framelayout = (FrameLayout) findViewById(R.id.fragment2_content);
        framelayout2 = (FrameLayout) findViewById(R.id.fragment2_content2);
//        ReplaceFragmentMethod();

        firstFragment = new Fragment_First();
        secondFragment = new Fragment_Second();
        thirdFragment = new Fragment_Third();

        ViewTreeObserver vto = framelayout.getViewTreeObserver();
        vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
                    framelayout.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                } else {
                    framelayout.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                }
                width  = framelayout.getMeasuredWidth();
                height = framelayout.getMeasuredHeight();

                Log.e("onGlobalLayout===", width+"==="+height);

            }
        });


        Log.e("onCreate===", framelayout.getMeasuredWidth()+"==="+framelayout.getMeasuredHeight());

        mLoopRotarySwitchView = (LoopRotarySwitchView) findViewById(R.id.mLoopRotarySwitchView);

        mLoopRotarySwitchView
                .setR(300)//设置R的大小
                .setAutoRotation(false)//是否自动切换
                .setAutoScrollDirection(LoopRotarySwitchView.AutoScrollDirection.left)//切换方向
                .setAutoRotationTime(2000);//自动切换的时间  单位毫秒

//        mLoopRotarySwitchView.setAngle(45f);

//        mLoopRotarySwitchView.setX(100f);
//        mLoopRotarySwitchView.setY(100f);
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
//            mLoopRotarySwitchView.setZ(90f);
//            Log.e("onCreate===", "===");
//        }

//        mLoopRotarySwitchView.setRotationX(0.5f);

        mLoopRotarySwitchView.setLoopRotationX(-10);
//        mLoopRotarySwitchView.setLoopRotationY(50);

//        mLoopRotarySwitchView.setB

//        mLoopRotarySwitchView.getL

        mLoopRotarySwitchView.setOnItemSelectedListener(new OnItemSelectedListener() {
            @Override
            public void selected(int position, View view) {

                Log.e("selected===", pos+"==="+position+"==="+dx);


//                isEnd = true;
                if(dx!=0){
//                    dx = 0;
//                    isEnd = false;
//                    first = true;

//                    return;
                }else{
                    firstFragment = new Fragment_First();
                    secondFragment = new Fragment_Second();
                    thirdFragment = new Fragment_Third();

                    if(pos==0 && position==1){
                        applyRotation(true, firstFragment, secondFragment,0, 90);
                    }else if(pos==1 && position==2){
                        applyRotation(true, secondFragment, thirdFragment,0, 90);
                    }else if(pos==2 && position==0){
                        applyRotation(true, thirdFragment, firstFragment,0, 90);
                    }else if(pos==0 && position==2){
                        applyRotation(false, firstFragment, thirdFragment,0, 90);
                    }else if(pos==2 && position==1){
                        applyRotation(false, thirdFragment, secondFragment,0, 90);
                    }else if(pos==1 && position==0){
                        applyRotation(false, secondFragment, firstFragment,0, 90);
                    }
                }

                pos = position;

//                first = true;
//                isEnd = false;
            }
        });

        mLoopRotarySwitchView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(int item, View view) {

                TextView tv = (TextView)view;
                Log.e("onItemClick===", item+"==="+tv.getText());

//                if(item==1){
//                    applyRotation(true, thirdFragment, secondFragment,0, 90);
//                }else{
//                    applyRotation(true, thirdFragment, secondFragment,0, 90);
//                }

//                tration = getSupportFragmentManager().beginTransaction();
//
////                fromFragment = firstFragment;
////                tration.replace(R.id.fragment_content, new Fragment_Second());
////                applyRotation(true, new Fragment_Second(), 0, 90);
//
//                fromFragment = secondFragment;
//                tration.replace(R.id.fragment_content2, new Fragment_First());
//                applyRotation(true, new Fragment_First(), 0, 90);
//
//                tration.commit();
            }
        });

        mLoopRotarySwitchView.setOnLoopViewTouchListener(new OnLoopViewTouchListener() {
            @Override
            public void onTouch(MotionEvent event) {

                if(event.getAction()==0){
                    dx = 0;
                    first = true;
                    isEnd = false;
                }

                Log.e("onTouch===0", first+"==="+isEnd+"==="+dx+"==="+x+"==="+event.getX()+"==="+event.getY()+"==="+event.getAction());

                if(first){


                    x = event.getX();
                    first = false;
//


                }

                if(isEnd){
                    return;
                }

//                TextView tv = (TextView)(mLoopRotarySwitchView.getFocusedChild());

//                Log.e("onTouch===2", event.getAction()+"==="+mLoopRotarySwitchView.getSelectItem()+"==="+mLoopRotarySwitchView.getNextFocusUpId());

//                dx = event.getX()-x;
                dx = (x-event.getX())/200;



                Log.e("onTouch===", pos+"==="+dx+"==="+x+"==="+event.getX()+"==="+event.getY()+"==="+event);


//                tration = getSupportFragmentManager().beginTransaction();

                if(event.getAction()==1){   //手指离开屏幕
                    isEnd = true;
                    first = true;
//                    applyRotation(true, new Fragment_Second(), 0, 90);
                }else{
                    isEnd = false;
//                    applyRotation(true, new Fragment_Second(), 0, 90);
                }

                if(dx==0){
                    return;
                }else{
                    firstFragment = new Fragment_First();
                    secondFragment = new Fragment_Second();
                    thirdFragment = new Fragment_Third();
                }

                if(dx>0.1){
//                    fromFragment = firstFragment;
//                    tration.replace(R.id.fragment_content, new Fragment_Second());
//                    tration.replace(R.id.fragment_content, secondFragment);

                    Log.e("onTouch===1", tration+"==="+pos);

                    if(pos==0){
                        applyRotation(true, firstFragment, secondFragment,0, 90);
                    }else if(pos==1){
                        applyRotation(true, secondFragment, thirdFragment,0, 90);
//                        applyRotation(true, firstFragment, thirdFragment,0, 90);
                    }else{
                        applyRotation(true, thirdFragment, firstFragment,0, 90);
                    }

                }else if(dx<-0.1){
//                    fromFragment = firstFragment;
//                    tration.replace(R.id.fragment_content, new Fragment_Second());

                    Log.e("onTouch===2", tration+"==="+pos);

                    if(pos==0){
                        applyRotation(true, firstFragment, thirdFragment,0, 90);
                    }else if(pos==1){

                        applyRotation(true, secondFragment, firstFragment,0, 90);
                    }else{
                        applyRotation(true, thirdFragment, secondFragment,0, 90);
                    }
                }


//                tration.commit();


                degrees = Util_Rotate3DAnimation2.degrees;
                degrees2 = Util_Rotate3DAnimation2.degrees2;
                Log.e("onTouch===3", isEnd+"==="+dx+"==="+degrees+"==="+degrees2+"==="+mLoopRotarySwitchView.getSelectItem()+"==="+event.getX()+"==="+event.getY()+"==="+event.getAction());


                if(rotation!=null){
//                    degrees = rotation.getDegrees();

                }

            }
        });

        mLoopRotarySwitchView.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                TextView tv = (TextView)v;
                Log.e("onFocusChange===", tv.getText()+"==="+hasFocus);
            }
        });

//        mLoopRotarySwitchView.setOnTouchListener(new View.OnTouchListener() {
//            @Override
//            public boolean onTouch(View v, MotionEvent event) {
//                LoopRotarySwitchView rv = (LoopRotarySwitchView)v;
//                Log.e("onTouch===#", rv.getSelectItem()+"==="+event.getAction());
//                return false;
//            }
//        });

        mLoopRotarySwitchView.setOnHoverListener(new View.OnHoverListener() {
            @Override
            public boolean onHover(View v, MotionEvent event) {
                Log.e("onHover===", v+"==="+event.getAction());
                return false;
            }
        });

        mLoopRotarySwitchView.requestFocus();
    }


    private void ReplaceFragmentMethod() {
        FragmentTransaction tration = getSupportFragmentManager().beginTransaction();
        tration.replace(R.id.fragment2_content, new Fragment_First());
        tration.commit();
    }


    public void applyRotation(final boolean zheng, Fragment fromFragment, Fragment toFragment, float start, float end) {
        // Find the center of the container
//        final float centerX = framelayout.getWidth() / 2.0f;
//        final float centerX = framelayout.getWidth() / 2.0f;
//        final float centerY = framelayout.getHeight();
//        final float centerX = framelayout.getWidth() * 2.0f;

//        final float centerX = framelayout.getWidth();
//        final float centerY = framelayout.getHeight();
//        start = 0;
//        end = -90;

//        final float centerX = width;
        float centerX;
        float centerY;

        if(dx==0){
            if(zheng){
                //下一页逐渐显示
                FragmentTransaction tration = getSupportFragmentManager().beginTransaction();
                tration.replace(R.id.fragment2_content, toFragment);
                tration.commit();

                centerX = 0;
                centerY = 0;
                start = 30;
                end = 0;

                Log.e("applyRotation===", isEnd+"==="+dx+"==="+degrees+"==="+centerX+"==="+centerY+"==="+framelayout.getWidth()+"==="+framelayout.getHeight());

                // Create a new 3D rotation with the supplied parameter
                // The animation listener is used to trigger the next animation
                Util_Rotate3DAnimation2 rotation = new Util_Rotate3DAnimation2(framelayout, width, dx, start, end, centerX, centerY, 0.0f, zheng, true, isEnd);
                rotation.setDuration(1000);
                rotation.setFillAfter(true);
                rotation.setInterpolator(new AccelerateInterpolator());
//        rotation.setAnimationListener(new DisplayNextView(0, 0, zheng, new Fragment_First()));// 添加监听执行现实内容的切换
//        rotation.setAnimationListener(new DisplayNextView(width/2, 0, zheng, new Fragment_Second()));
                framelayout.startAnimation(rotation);// 执行上半场翻转动画


                Log.e("applyRotation===1", isEnd+"==="+dx+"==="+degrees2+"==="+centerX+"==="+centerY+"==="+framelayout.getWidth()+"==="+framelayout.getHeight());

                //当前页逐渐关闭
                tration = getSupportFragmentManager().beginTransaction();
                tration.replace(R.id.fragment2_content2, fromFragment);
                tration.commit();

//        centerX = isEnd?width*(1-dx):width;
                centerX = width;
                centerY = 0;
                start = 0;
                end = -90;

                Log.e("applyRotation===2", "==="+centerX+"==="+centerY+"==="+framelayout.getWidth()+"==="+framelayout.getHeight());

                // Create a new 3D rotation with the supplied parameter
                // The animation listener is used to trigger the next animation
                Util_Rotate3DAnimation2 rotation2 = new Util_Rotate3DAnimation2(framelayout2, width, dx, start, end, centerX, centerY, 0.0f, zheng, false, isEnd);
                rotation2.setDuration(1000);
                rotation2.setFillAfter(true);
                rotation2.setInterpolator(new AccelerateInterpolator());
//        rotation.setAnimationListener(new DisplayNextView(zheng, fragment));// 添加监听执行现实内容的切换
                framelayout2.startAnimation(rotation2);// 执行上半场翻转动画
            }else{
                //当前页逐渐关闭
                FragmentTransaction tration = getSupportFragmentManager().beginTransaction();
                tration.replace(R.id.fragment2_content, fromFragment);
                tration.commit();

                centerX = 0;
                centerY = 0;
                start = 0;
                end = 90;
//        start = isEnd?degrees:30;
//        end = 0;

                Log.e("applyRotation===", isEnd+"==="+dx+"==="+degrees+"==="+centerX+"==="+centerY+"==="+framelayout.getWidth()+"==="+framelayout.getHeight());

                // Create a new 3D rotation with the supplied parameter
                // The animation listener is used to trigger the next animation
                Util_Rotate3DAnimation2 rotation = new Util_Rotate3DAnimation2(framelayout, width, dx, start, end, centerX, centerY, 0.0f, zheng, true, isEnd);
                rotation.setDuration(1000);
                rotation.setFillAfter(true);
                rotation.setInterpolator(new AccelerateInterpolator());
//        rotation.setAnimationListener(new DisplayNextView(0, 0, zheng, new Fragment_First()));// 添加监听执行现实内容的切换
//        rotation.setAnimationListener(new DisplayNextView(width/2, 0, zheng, new Fragment_Second()));
                framelayout.startAnimation(rotation);// 执行上半场翻转动画


                Log.e("applyRotation===1", isEnd+"==="+dx+"==="+degrees2+"==="+centerX+"==="+centerY+"==="+framelayout2.getWidth()+"==="+framelayout2.getHeight());

                //下一页逐渐显示
                tration = getSupportFragmentManager().beginTransaction();
                tration.replace(R.id.fragment2_content2, toFragment);
                tration.commit();

//        centerX = isEnd?width*(1-dx):width;
                centerX = width;
                centerY = 0;
                start = -30;
                end = 0;

//        Log.e("applyRotation===2", "==="+centerX+"==="+centerY+"==="+framelayout.getWidth()+"==="+framelayout.getHeight());

                // Create a new 3D rotation with the supplied parameter
                // The animation listener is used to trigger the next animation
                Util_Rotate3DAnimation2 rotation2 = new Util_Rotate3DAnimation2(framelayout2, width, dx, start, end, centerX, centerY, 0.0f, zheng, false, isEnd);
                rotation2.setDuration(1000);
//        rotation2.setDuration(3000);
                rotation2.setFillAfter(true);
                rotation2.setInterpolator(new AccelerateInterpolator());
//        rotation.setAnimationListener(new DisplayNextView(zheng, fragment));// 添加监听执行现实内容的切换
                framelayout2.startAnimation(rotation2);// 执行上半场翻转动画
            }

        }else if(dx>0 && dx<1){       //右转
            //下一页逐渐显示
            FragmentTransaction tration = getSupportFragmentManager().beginTransaction();
            tration.replace(R.id.fragment2_content, toFragment);
            tration.commit();

            centerX = 0;
            centerY = 0;
            start = isEnd?degrees:30;
            end = 0;

            Log.e("applyRotation===", isEnd+"==="+dx+"==="+degrees+"==="+centerX+"==="+centerY+"==="+framelayout.getWidth()+"==="+framelayout.getHeight());

            // Create a new 3D rotation with the supplied parameter
            // The animation listener is used to trigger the next animation
            Util_Rotate3DAnimation2 rotation = new Util_Rotate3DAnimation2(framelayout, width, dx, start, end, centerX, centerY, 0.0f, zheng, true, isEnd);
            rotation.setDuration((long)(1000*(1-dx)));
            rotation.setFillAfter(true);
            rotation.setInterpolator(new AccelerateInterpolator());
//        rotation.setAnimationListener(new DisplayNextView(0, 0, zheng, new Fragment_First()));// 添加监听执行现实内容的切换
//        rotation.setAnimationListener(new DisplayNextView(width/2, 0, zheng, new Fragment_Second()));
            framelayout.startAnimation(rotation);// 执行上半场翻转动画


            Log.e("applyRotation===1", isEnd+"==="+dx+"==="+degrees2+"==="+centerX+"==="+centerY+"==="+framelayout.getWidth()+"==="+framelayout.getHeight());

            //当前页逐渐关闭
            tration = getSupportFragmentManager().beginTransaction();
            tration.replace(R.id.fragment2_content2, fromFragment);
            tration.commit();

//        centerX = isEnd?width*(1-dx):width;
            centerX = width;
            centerY = 0;
            start = isEnd?degrees2:0;
            end = -90;

            Log.e("applyRotation===2", "==="+centerX+"==="+centerY+"==="+framelayout.getWidth()+"==="+framelayout.getHeight());

            // Create a new 3D rotation with the supplied parameter
            // The animation listener is used to trigger the next animation
            Util_Rotate3DAnimation2 rotation2 = new Util_Rotate3DAnimation2(framelayout2, width, dx, start, end, centerX, centerY, 0.0f, zheng, false, isEnd);
            rotation2.setDuration((long)(1000*(1-dx)));
            rotation2.setFillAfter(true);
            rotation2.setInterpolator(new AccelerateInterpolator());
//        rotation.setAnimationListener(new DisplayNextView(zheng, fragment));// 添加监听执行现实内容的切换
            framelayout2.startAnimation(rotation2);// 执行上半场翻转动画
        }else if(dx<0 && dx>=-1){  //左转
            //当前页逐渐关闭
            FragmentTransaction tration = getSupportFragmentManager().beginTransaction();
            tration.replace(R.id.fragment2_content, fromFragment);
            tration.commit();

            centerX = 0;
            centerY = 0;
            start = isEnd?-degrees:0;;
            end = 90;
//        start = isEnd?degrees:30;
//        end = 0;

            Log.e("applyRotation===", isEnd+"==="+dx+"==="+degrees+"==="+centerX+"==="+centerY+"==="+framelayout.getWidth()+"==="+framelayout.getHeight());

            // Create a new 3D rotation with the supplied parameter
            // The animation listener is used to trigger the next animation
            Util_Rotate3DAnimation2 rotation = new Util_Rotate3DAnimation2(framelayout, width, dx, start, end, centerX, centerY, 0.0f, zheng, true, isEnd);
            rotation.setDuration((long)(1000*(1+dx)));
            rotation.setFillAfter(true);
            rotation.setInterpolator(new AccelerateInterpolator());
//        rotation.setAnimationListener(new DisplayNextView(0, 0, zheng, new Fragment_First()));// 添加监听执行现实内容的切换
//        rotation.setAnimationListener(new DisplayNextView(width/2, 0, zheng, new Fragment_Second()));
            framelayout.startAnimation(rotation);// 执行上半场翻转动画


            Log.e("applyRotation===1", isEnd+"==="+dx+"==="+degrees2+"==="+centerX+"==="+centerY+"==="+framelayout2.getWidth()+"==="+framelayout2.getHeight());

            //下一页逐渐显示
            tration = getSupportFragmentManager().beginTransaction();
            tration.replace(R.id.fragment2_content2, toFragment);
            tration.commit();

//        centerX = isEnd?width*(1-dx):width;
            centerX = width;
            centerY = 0;
            start = isEnd?degrees2:-30;
            end = 0;

//        Log.e("applyRotation===2", "==="+centerX+"==="+centerY+"==="+framelayout.getWidth()+"==="+framelayout.getHeight());

            // Create a new 3D rotation with the supplied parameter
            // The animation listener is used to trigger the next animation
            Util_Rotate3DAnimation2 rotation2 = new Util_Rotate3DAnimation2(framelayout2, width, dx, start, end, centerX, centerY, 0.0f, zheng, false, isEnd);
            rotation2.setDuration((long)(1000*(1+dx)));
//        rotation2.setDuration(3000);
            rotation2.setFillAfter(true);
            rotation2.setInterpolator(new AccelerateInterpolator());
//        rotation.setAnimationListener(new DisplayNextView(zheng, fragment));// 添加监听执行现实内容的切换
            framelayout2.startAnimation(rotation2);// 执行上半场翻转动画
        }






    }


    private final class DisplayNextView implements Animation.AnimationListener {
        private final boolean mPosition;
        private final Fragment mfragment;

        float centerX;
        float centerY;

        private DisplayNextView(float x, float y, boolean zheng, Fragment fragment) {
            centerX = x;
            centerY = y;
            mPosition = zheng;
            mfragment = fragment;
        }

        public void onAnimationStart(Animation animation) {
            framelayout.post(new SwapViews(centerX, centerY, mPosition, mfragment));// 添加新的View
        }

        public void onAnimationEnd(Animation animation) {

        }

        public void onAnimationRepeat(Animation animation) {
        }
    }


    private final class SwapViews implements Runnable {
        private final boolean mPosition;
        private final Fragment mfragment;
        float centerX;
        float centerY;

        public SwapViews(float x, float y, boolean position, Fragment fragment) {
            centerX = x;
            centerY = y;
            mPosition = position;
            mfragment = fragment;
        }

        public void run() {
//            final float centerX = framelayout.getWidth() / 2.0f;
//            final float centerY = framelayout.getHeight() ;
//            float centerX = -framelayout.getWidth();
//            float centerY = 0;
            Util_Rotate3DAnimation2 rotation;
            FragmentTransaction tration = getSupportFragmentManager().beginTransaction();
            tration.replace(R.id.fragment2_content, mfragment);

            Log.e("run===", mPosition+"==="+centerX+"==="+centerY);

//            centerX = 0;
//            centerY = 0;
//            start = 0;
//            end = 90;

//            if (mPosition) {
//                rotation = new Util_Rotate3DAnimation2(framelayout, width, dx,0, 90, centerX, centerY, 0.0f, zheng, false, isEnd);
//            } else {
//                rotation = new Util_Rotate3DAnimation2(framelayout, width, dx,90, 0, centerX, centerY, 310.0f, zheng, false, isEnd);
//            }
//            tration.commit();
//            rotation.setDuration(1000);
//            rotation.setFillAfter(true);
//            rotation.setInterpolator(new DecelerateInterpolator());
//            framelayout.startAnimation(rotation);
        }
    }


}
